package ru.aptu.gsp_banking.dao_impl;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import ru.aptu.gsp_banking.dao.AccountDAO;
import ru.aptu.gsp_banking.domain.Account;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: alex
 * Date: 11/16/11
 * Time: 3:58 PM
 */

@Repository
public class AccountDAOImpl implements AccountDAO {

    @Autowired
	private SessionFactory sessionFactory;

    public List<Account> listAccount(String currentUser) {

        return (List<Account>) sessionFactory.getCurrentSession().createQuery("from Account where username='" + currentUser + "'")
			.list();
    }

    public Account getAccountById(Integer accountId) {
        return (Account) sessionFactory.getCurrentSession()
                .createQuery("from Account where id=" + accountId.toString())
                .uniqueResult();
    }

    public boolean checkAccount(Integer accountId, String name) {
        Account queryResult = (Account) sessionFactory.getCurrentSession()
                .createQuery("from Account where id=" + Integer.toString(accountId) + " and username='" + name + "')")
                .uniqueResult();

        return (queryResult != null);
    }

    public String getAccountNumber(Integer accountId) {
        Account account = (Account) sessionFactory.getCurrentSession()
                .createQuery("from Account where id=" + Integer.toString(accountId)).uniqueResult();
        return account.getAccountNumber();
    }
}
